<?php

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

require_once __DIR__ . '/../../vendor/autoload.php';

$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->queue_declare('rpc', false, false, false, false);
function fib($n) {
		if ($n == 0) {
				return 0;
		}
		if ($n == 1) {
				return 1;
		}
		return fib($n - 1) + fib($n - 2);
}
$callback = function ($req) {
		// var_dump($req);
		$n = intval($req->body);
		echo 'fib(', $n, ')', PHP_EOL;
		$msg = new AMQPMessage(fib($n), ['correlation_id' => $req->get('correlation_id')]);
		$req->delivery_info['channel']->basic_publish($msg, '', $req->get('reply_to'));
		$req->ack();
};
// $msg = new AMQPMessage($data);
// $channel->basic_publish($msg, 'topic-logs', $routing_key);
$channel->basic_qos(null, 1, null);
$channel->basic_consume('rpc', '', false, false, false, false, $callback);
while ($channel->is_open()) {
		$channel->wait();
}
$channel->close();
$connection->close();
